﻿<%@ Page Language="C#" %>
<%@ Register TagPrefix="IRL" TagName="SideBar" Src="SideBar.ascx" %>
<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">        
    public const int CURRENT_MODULE = 54;
    
    protected void Page_Load(object src, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();
        
        // load reminder count        
        //Mics.loadReminderCount(lblReminderCount);

        SideBar1.loadData();
        if (!Page.IsPostBack)
        {
            rebind();
        }
    }

    // bind to datagrid
    void rebind()
    {
        string market = "";
        if (Request.Cookies["office"] != null)
            market = Request.Cookies["office"].Value;
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using(SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("sp_ListReminders", conn);
            SqlDataReader dr;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@userid", SqlDbType.Int).Value = Request.Cookies["irl_id"].Value;
            cmd.Parameters.Add("@type", SqlDbType.VarChar, 10).Value = "Billing";
            if (market != "")
                cmd.Parameters.Add("@market", SqlDbType.Int).Value = market;
            conn.Open();
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            // bound to datagrid
            grid1.DataSource = dr;
            grid1.DataBind();
            // close reader
            dr.Close();
        }
    }    
    
    // showDueIn
    private string showDueIn(object DueDate)
    {
        TimeSpan diff = DateTime.Today - (DateTime)DueDate;
        if (diff.Days == 0)
        {
            return "<b>Today</b>";
        }
        else if ((diff.Days % 7) == 0)
        {
            int diffWeeks = diff.Days / 7;
            if (diffWeeks == 1)
                return "<b>1</b> week overdue";
            else
                return "<b>" + diffWeeks + "</b> weeks overdue";
        }
        else
        {
            return "<b>" + diff.Days + "</b> days overdue";
        }
    }
    
    // showReminderType
    private string showReminderType(object value)
    {
        int ReminderType = (int)value;
        string strMsg = "";

        if (ReminderType == 1)
        {
            strMsg = " (issue invoice)";
        }
        else if (ReminderType == 2)
        {
            strMsg= " (follow up)";
        }

        return strMsg;
    }

    protected void grid1_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int milestoneID, reminderType;

        milestoneID = Convert.ToInt32(e.Item.Cells[0].Text);
        reminderType = Convert.ToInt32(e.Item.Cells[1].Text);

        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            if (e.CommandName == "Mark Complete")
            {
                SqlCommand cmd = new SqlCommand("UPDATE [cms_Reminders] SET [IsEnabled]=0 WHERE [MilestoneID]=" + milestoneID + " AND [ReminderType]=" + reminderType, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            
            SideBar1.refreshReminder();
            rebind();
        }
    }

    protected void grid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton lnk = (LinkButton)e.Item.FindControl("cmdMarkComplete");
            lnk.Attributes.Add("onclick", "return markComplete();");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>CMS - Billing Reminders</title>
    <link type="text/css" rel="stylesheet" href="../css/global.css" />
    <link type="text/css" rel="stylesheet" href="css/cms.css" />    
    
    <script language="javascript">
        function markComplete(){
			var strMsg = "Are you sure you want to mark complete for this item?\n\n" +
							"=========================================\n\n" +							
							"Click OK to continue, or Cancel to halt the execution.";
			return confirm(strMsg);
		}
    </script>

</head>

<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="540" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">&nbsp;</td>
					<td><img src="images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="padding:0 20px;">
						<div style="background-color:#bcb3d5; padding:5px 10px;">
							<span style="float:right">

								&nbsp;</span>
							<span class="pathmap">
								<a href="default.aspx" >CPanel</a> :: 
								<a href="default2.aspx" >Ops Files</a> :: 
								Billing Reminders
						  </span>
						</div>
						<br />
						
						
					</td>
				</tr>
				
			</table>
			
			<!-- body -->
			<form id="DataForm" method="post" runat="server">
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" valign="top" style="padding-left:20px;">
						<table align="left" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td valign="top">
								
									<IRL:SideBar ID="SideBar1" runat="server" />
								
								</td>
								<td width="10"><img src="images/spacer.gif" width="10" height="1" /></td>
								<td valign="top">
								    <!-- data row -->
                                    <div style="width:480px; overflow:auto; overflow-y:visible">
									    <asp:DataGrid CssClass="data" ID="grid1" Width="100%" runat="server" CellSpacing="1" CellPadding="5"
                                            BorderWidth="0" GridLines="none" AutoGenerateColumns="false" OnItemCommand="grid1_ItemCommand"
                                            OnItemDataBound="grid1_ItemDataBound">
                                            <HeaderStyle CssClass="header" />
                                            <ItemStyle BackColor="#efefef" />
                                            <AlternatingItemStyle BackColor="white" />
                                            <Columns>
                                                <asp:BoundColumn DataField="MilestoneID" Visible="false" />
                                                <asp:BoundColumn DataField="ReminderType" Visible="false" />
                                                
                                                <asp:TemplateColumn HeaderText="No">
                                                    <HeaderStyle Width="20" />
                                                    <ItemTemplate>
                                                        <%# Container.ItemIndex+1%>
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                                <asp:TemplateColumn HeaderText="Project Name">
                                                    <HeaderStyle Width="220" />
                                                    <ItemTemplate>
                                                        <a class="edit" href="listMilestones.aspx?pid=<%# DataBinder.Eval(Container.DataItem, "ProposalID").ToString() %>" >
                                                            <%# DataBinder.Eval(Container.DataItem, "ProjectName").ToString() %></a>
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                                
                                                <asp:TemplateColumn HeaderText="Milestone Type">
                                                    <HeaderStyle Width="220" />
                                                    <ItemTemplate>
                                                        <%# DataBinder.Eval(Container.DataItem, "MilestoneType").ToString() %>
                                                        <%# showReminderType(DataBinder.Eval(Container.DataItem, "ReminderType"))%>
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                                <asp:TemplateColumn HeaderText="Due in">
                                                    <HeaderStyle Width="120" />
                                                    <ItemTemplate>
                                                        <%# showDueIn(DataBinder.Eval(Container.DataItem, "ReminderDate"))%>
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                                <asp:ButtonColumn Text="Mark Complete" Visible="false" />
                                                <asp:TemplateColumn HeaderText="Action" Visible="true">
                                                    <HeaderStyle Width="120" />
                                                    <ItemTemplate>
                                                        <asp:LinkButton ID="cmdMarkComplete" CommandName="Mark Complete" Text="Mark Complete"
                                                            runat="server" />
                                                    </ItemTemplate>
                                                </asp:TemplateColumn>
                                            </Columns>
                                        </asp:DataGrid>
                                    </div>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			</form>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>


</body>
</html>
